using System;
using InfoJetSoft.Service.Worker;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.ManagedCode
{
	/// <summary>
	/// Summary description for EmailAdapter.
	/// </summary>
	public class EmailAdapter : Microsoft.Office.Interop.InfoPath.SemiTrust.EmailAdapterObject
	{
		private InfoJetSoft.Service.Entity.Form form = null;
		private InfoJetSoft.Service.Entity.DataEmailAdapter emailAdapter = null;

		public EmailAdapter( InfoJetSoft.Service.Entity.Form frm, InfoJetSoft.Service.Entity.DataEmailAdapter adapter )
		{
			this.form = frm;
			this.emailAdapter = (InfoJetSoft.Service.Entity.DataEmailAdapter)DataAdapterTableUtil.getCurrentAdapter( this.form, adapter );
		}
		
		public bool QueryAllowed 
		{ 
			get
			{
				return this.emailAdapter.QueryAllowed;
			} 
		}

		public bool SubmitAllowed 
		{ 
			get
			{
				return this.emailAdapter.QueryAllowed;
			}
		}

		public string Name 
		{ 
			get
			{
				return this.emailAdapter.Name;
			}	
		}

		public string To 
		{ 
			get
			{
				if( this.emailAdapter.To == null )
				{
					return DataObjectEmailSubmitWorker.getMailTo( this.form, this.emailAdapter );
				}
				else
				{
					return this.emailAdapter.To;
				}
			}
			set
			{
				this.emailAdapter.To = value;
			}
		}

		public string CC 
		{ 
			get
			{
				if( this.emailAdapter.Cc == null )
				{
					return DataObjectEmailSubmitWorker.getMailCc( this.form, this.emailAdapter );
				}
				else
				{
					return this.emailAdapter.Cc;
				}
			}
			set
			{
				this.emailAdapter.Cc = value;
			}
		}

		public string BCC 
		{ 
			get
			{
				if( this.emailAdapter.Bcc == null )
				{
					return DataObjectEmailSubmitWorker.getMailBcc( this.form, this.emailAdapter );
				}
				else
				{
					return this.emailAdapter.Bcc;
				}
			}
			set
			{
				this.emailAdapter.Bcc = value;
			}
		}

		public string Subject 
		{ 
			get
			{
				if( this.emailAdapter.Subject == null )
				{
					return DataObjectEmailSubmitWorker.getMailSubject( this.form, this.emailAdapter );
				}
				else
				{
					return this.emailAdapter.Subject;
				}
			}
			set
			{
				this.emailAdapter.Subject = value;
			}
		}

		public string AttachmentFileName 		
		{ 
			get
			{
				if( this.emailAdapter.AttachmentFileName == null )
				{
					return DataObjectEmailSubmitWorker.getMailAttachName( this.form, this.emailAdapter );
				}
				else
				{
					return this.emailAdapter.AttachmentFileName;
				}
			}
			set
			{
				this.emailAdapter.AttachmentFileName = value;
			}
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.XdAttachmentType AttachmentType 
		{
			get
			{
				return Microsoft.Office.Interop.InfoPath.SemiTrust.XdAttachmentType.xdXml;
			}
			set
			{
			
			}
		}

		public string Intro
		{ 
			get
			{
				return this.emailAdapter.IntroValue;
			}
			set
			{
				this.emailAdapter.IntroValue = value;
			}
		}

		public void Submit()
		{
            //CID 0859 //2012.03.19 //Begin
            int errorCount = FormIsValidateWorker.Validate(form);
            if (errorCount > 0)
            {
                throw new InfoJetException(MessageUtil.FormContainsValidationErrors(), null);
            }
            //CID 0859 //2012.03.19 //End
			DataObjectEmailSubmitWorker.submitDoc( this.form, this.emailAdapter );
		}

		//Unsupported
		public void Query(){}
		public void SubmitData(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode pData){}

	}
}
